package com.jeecg.study.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.jeecg.study.action.ActionNew;
import com.jeecg.study.common.utils.Global;
import com.jeecg.study.entity.XmlBeans;
import com.jeecg.study.form.ActionForm;
import com.jeecg.study.form.FullForm;
import com.jeecg.study.listeners.ActionListeners;

/**
 * Servlet implementation class ServletAction
 */
//@WebServlet("*.action")
public class ServletAction extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public static Map<String,XmlBeans> mapNew=null;

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletAction() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = getPrefix(request.getServletPath());
		System.out.println(url);
		XmlBeans xmlBeans = mapNew.get(url);
		if(null == xmlBeans){
			// 请求url在 jeecgMVC.xml 中未进行配置
			request.getRequestDispatcher(getForward(url, mapNew.get("error-404"))).forward(request, response);;
			return;
		}
		String formDataClass = xmlBeans.getFormDataClass();
		ActionForm actionForm = null;
		if(StringUtils.isNotBlank(formDataClass)){
			actionForm = FullForm.bindForm(formDataClass, request);
		}
		String actionClass = xmlBeans.getActionClass();
		if(StringUtils.isNotBlank(actionClass)){
			try {
				Class  clzz=Class.forName(actionClass);
				ActionNew action =(ActionNew) clzz.newInstance();
				String resultUrl = action.execute(request, response, actionForm, xmlBeans.getResultMap());
				request.getRequestDispatcher(resultUrl).forward(request, response);;
				return ;
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		request.getRequestDispatcher(getForward(url,xmlBeans)).forward(request, response);;
	}
	
	/**
	 * @see 获取最终转发的url
	 * @param request
	 * @param xmlBeans
	 * @return
	 */
	private String getForward(String url, XmlBeans xmlBeans) {
		if(xmlBeans.getResultMap().size() == 1){
			url = xmlBeans.getResultMap().get(Global.RESULT_SUCCESS);
			return url==null?xmlBeans.getResultMap().get(null):url;
		}else{
			//多个resultMap，则需要执行对应的service才能进行处理
		}
		return null;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * @see 获取请求路径前缀， 去掉servlet 过滤的后缀 *.xx
	 * @param path
	 * @return
	 */
	private String getPrefix(String path){
		// ".action" 为 servelt 过滤的后缀,后续调整到配置文件中
		return path.replace(Global.SERVLET_URL_PATTERN, "");
	}

}
